Technical Q&As


QTW 73 - Creating a PicHandle for a PICT Stored in Global Memory (09-Feb-96)


Q We are developing a client/server application. The server app is on a Macintosh and the client app uses a Windows machine. Therefore, we need to create a PicHandle for a PICT stored in global memory. How is this done?

A Here is a code sample to do just that.

Note:
This will only work for a true PICT, not for all JPEG/JFIF images that can be loaded with OpenPictureFile/GetPictureFromFile.
// ---------------------------------------------------------------------
//
// Pict2DIB.c - Sample QuickTime for Windows Application
//
// Reads the PICT data from the PICT file specified on the command
// line into global memory, casts the PicHandle to access the PICT, and 
// demonstrates using the PicHandle to create a .BMP file containing a 
// DIB.
//
// dib.h and the WriteDIB function are available from Microsoft on the 
// Microsoft Visual C++ and Microsoft Developer Network CD-ROMS.
//

//  Copyright 1988-1996 Apple Computer, Inc. All Rights Reserved.
//
// ---------------------------------------------------------------------
 
static char szAppName[] = "Pict2DIB";
 
#include <windows.h>#include <qtw.h>#include "dib.h"
 
#define MB_Ret(msg, code)  \
   { MessageBox (NULL, msg, szAppName, MB_OK); return code; }
 
 
int PASCAL WinMain (HINSTANCE hInstance,    HINSTANCE hPrevInstance,
                    LPSTR     lpszCmdParam, int       CmdShow)
   {
   HFILE       hfile;
   DWORD       lSize;
   HANDLE      hGlobalMem;
   char huge * hpGlobalMem;
   DIBHandle   hdPicture;
 
   // Open file and find its size
   hfile = _lopen (lpszCmdParam, READ);
   if (hfile==HFILE_ERROR) MB_Ret ("_lopen failure", 0);
   lSize = _llseek (hfile, 0, 2);
 
   // Allocate enough global memory for Pict data and obtain huge pointer
   hGlobalMem  = GlobalAlloc (GPTR, lSize-512);
   hpGlobalMem = (char huge *)GlobalLock (hGlobalMem);
 
   // Read Pict data from file into global memory and close file
   _llseek (hfile, 512L, 0);
   _hread  (hfile, hpGlobalMem, lSize-512);
   _lclose (hfile);
 
   // Cast global memory handle and use as PicHandle
   hdPicture = PictureToDIB ((PicHandle)hGlobalMem);
   WriteDIB ("pictout.bmp", hdPicture);
 
   // Unlock and free global memory
   GlobalUnlock (hGlobalMem);
   GlobalFree   (hGlobalMem);
 
   // Display MessageBox and exit
   MB_Ret (lpszCmdParam, TRUE);
   }
   


Technical Q&As
Previous Question | Contents | Next Question